package com.leo.chapter15;

/**
 * 模拟CAS操作
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class SimulatedCAS {
    private int value;

    public synchronized int get(){ return value; }
    public synchronized int compareAndSwap(int expectedValue, int newValue){
        int oldValue = value;
        if (oldValue == expectedValue){
            value = newValue;
        }
        return oldValue;
    }
    public synchronized boolean compareAndSet(int expectedValue, int newValue){
        return (compareAndSwap(expectedValue, newValue) == expectedValue);
    }
}
